//
//  SEGSecurityConfirmView.m
//  SEGBasic
//
//  Created by Clavis on 2019/5/24.
//  Copyright © 2019 刘山. All rights reserved.
//

#import "SEGSecurityConfirmView.h"

#if __has_include(<Commom/UIColor+YYAdd.h>)
#import <Commom/UIColor+YYAdd.h>
#else
#import "UIColor+YYAdd.h"
#endif

#if __has_include(<Commom/SEGColorManager.h>)
#import <Commom/SEGColorManager.h>
#else
#import "SEGColorManager.h"
#endif

#if __has_include(<Commom/SEGLayoutMacro.h>)
#import <Commom/SEGLayoutMacro.h>
#else
#import "SEGLayoutMacro.h"
#endif

#if __has_include(<Commom/SEGUtils.h>)
#import <Commom/SEGUtils.h>
#else
#import "SEGUtils.h"
#endif

#import "SEGText.h"

#import "SEGPlatformManager.h"

#import <Masonry/Masonry.h>

@implementation SEGSecurityConfirmView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.frame = [UIScreen mainScreen].bounds;
    self.backgroundColor = kColor_bg3;
    UIView *container = [[UIView alloc] init];
    container.backgroundColor = kColor_W2;
    container.layer.cornerRadius = 5;
    [self addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_offset(0);
        make.left.mas_equalTo(53);
        make.right.mas_equalTo(-53);
        make.height.equalTo(container.mas_width).multipliedBy(1.15);
    }];
    
    UILabel *titleLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [container addSubview:label];
        label.textColor = kColor_1;
        label.font = [UIFont boldSystemFontOfSize:16];
        label.text = @"用户协议、隐私政策概要";
        label;
    });
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(25);
        make.centerX.mas_offset(0);
    }];
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.showsVerticalScrollIndicator = NO;
    [container addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(25);
        make.right.mas_equalTo(-25);
        make.top.equalTo(titleLabel.mas_bottom).offset(14);
        make.bottom.mas_equalTo(-58);
    }];
    
    UILabel *contextLabel = ({
        UILabel *label = [[UILabel alloc] init];
        [scrollView addSubview:label];
        label.textColor = kColor_1;
        label.font = kFont_13;
        label.text = [NSString stringWithFormat:@"感谢您信任并使用%@！\n\n我们依据最新的法律要求更新了“用户协议”和“隐私政策”各条款，特向您推送本提示，请您仔细阅读并充分理解相关条款。", kAppName];
        label.numberOfLines = 0;
        label;
    });
    [contextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.centerX.mas_offset(0);
    }];
    
    UILabel *contextLabel2 = ({
        UILabel *label = [[UILabel alloc] init];
        [scrollView addSubview:label];
        label.textColor = kColor_1;
        label.font = kFont_13;
        label.text = @"1、为了帮助您手机开门、报单保修、在线缴费、浏览资讯、发布信息、注册认证，我们会收集您的部分必要信息；\n2、为提供上述服务，我们可能会搜集位置、相册等部分敏感信息，您有权拒绝或撤回授权；\n3、未经您同意，我们不会将上述信息用于您未授权的其他用途或目的；";
        label.numberOfLines = 0;
        label;
    });
    [contextLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contextLabel.mas_bottom).offset(10);
        make.left.equalTo(contextLabel);
        make.right.equalTo(contextLabel);
        make.centerX.mas_offset(0);
    }];
    
    UILabel *contextLabel3 = ({
        UILabel *label = [[UILabel alloc] init];
        [scrollView addSubview:label];
        label.textColor = kColor_1;
        label.font = kFont_13;
        label.text = @"【请注意】您点击同意即代表您已阅读并同意《用户协议》 和《隐私政策》 ，我们将竭尽全力保护您的个人信息及合法权益，再次感谢您的信任！";
        label.numberOfLines = 0;
        label;
    });
    [contextLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(contextLabel2.mas_bottom).offset(10);
        make.left.equalTo(contextLabel);
        make.right.equalTo(contextLabel);
        make.centerX.mas_offset(0);
    }];
    
    
    UIView *tipView = [[UIView alloc] init];
    [scrollView addSubview:tipView];
    [tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(contextLabel3.mas_bottom).offset(7);
        make.bottom.mas_equalTo(0);
        make.centerX.mas_offset(0);
        make.height.mas_equalTo(30);
    }];
    
    SEGLabel *privacyLabel = [[SEGLabel alloc] init];
    privacyLabel.textAlignment = NSTextAlignmentCenter;
    privacyLabel.numberOfLines = 0;
    NSString *str = @"前往查看完整版《用户协议》和《隐私政策》";
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:str];
    text.yy_font = kFont_13;
    text.yy_color = kColor_1;
    
    __weak typeof(self) weakSelf = self;
    //点击
    NSRange range = [str rangeOfString:@"《隐私政策》"];
    [text yy_setTextHighlightRange:range
                             color:kColor_S3
                   backgroundColor:[UIColor clearColor]
                         tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
        [weakSelf checkSecurityButtonTap:nil];
    }];
    NSRange range2 = [str rangeOfString:@"《用户协议》"];
    [text yy_setTextHighlightRange:range2
                             color:kColor_S3
                   backgroundColor:[UIColor clearColor]
                         tapAction:^(UIView *containerView, NSAttributedString *text, NSRange range, CGRect rect){
        [weakSelf checkUserAgreementTap:nil];
    }];
    [text yy_setUnderlineStyle:NSUnderlineStyleSingle range:range];
    [text yy_setUnderlineStyle:NSUnderlineStyleSingle range:range2];
    privacyLabel.attributedText = text;
    
    [scrollView addSubview:privacyLabel];
    [privacyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(contextLabel3.mas_bottom).offset(7);
        make.left.bottom.right.mas_equalTo(0);
    }];
    
    UIButton *disagreeButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"不同意" forState:UIControlStateNormal];
        [button setTitleColor:kColor_3 forState:UIControlStateNormal];
        button.titleLabel.font = kFont_16;
        [button addTarget:self action:@selector(disagreeButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [container addSubview:button];
        button;
    });
    [disagreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.mas_equalTo(0);
        make.width.equalTo(container.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(44);
    }];
    
    UIButton *agreeButton = ({
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@"同意" forState:UIControlStateNormal];
        [button setTitleColor:kColor_T forState:UIControlStateNormal];
        button.titleLabel.font = kFont_16;
        [button addTarget:self action:@selector(agreeButtonTap:) forControlEvents:UIControlEventTouchUpInside];
        [container addSubview:button];
        button;
    });
    [agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(0);
        make.width.equalTo(container.mas_width).multipliedBy(0.5);
        make.height.mas_equalTo(44);
    }];
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = kColor_L2;
    [container addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(0.5);
        make.bottom.equalTo(agreeButton.mas_top);
    }];
    
    UIView *verticalLineView = [[UIView alloc] init];
    verticalLineView.backgroundColor = kColor_L2;
    [container addSubview:verticalLineView];
    [verticalLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5);
        make.centerX.mas_offset(0);
        make.top.equalTo(lineView.mas_bottom);
        make.bottom.mas_equalTo(0);
    }];
}

- (void)checkSecurityButtonTap:(UIButton *)button {
    if (self.checkSecurityBlock) {
        self.checkSecurityBlock();
    }
}

- (void)checkUserAgreementTap:(UIButton *)button {
    if (self.checkUserAgreementBlock) {
        self.checkUserAgreementBlock();
    }
}

- (void)agreeButtonTap:(UIButton *)button {
    if (self.agreeBlock) {
        self.agreeBlock();
        [self dismiss];
    }
}

- (void)disagreeButtonTap:(UIButton *)button {
    if (self.disagreeBlock) {
        self.disagreeBlock();
    }
}

- (void)dismiss {
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
